import {defineStore} from "pinia";
import {ApiCartList, ApiGategories} from "@/api/commodity.js";
import {exchangeRates} from '@/api/common/index'

const useCartStore = defineStore(
'cart',
{
  state: () => ({
    isrRequest: false,
    shopCartTotal: 0,
    // 父级分类
    fatherCategoryList: [],
    //实时汇率
    realTimeRates: []
  }),
  actions: {
    // 获取购物车列表总数
    getCartList() {
      return ApiCartList().then(res => {
        this.shopCartTotal = res.total
        this.isrRequest = true
      })
    },
    // 清除购物车数量
    clearCartList() {
      this.shopCartTotal = 0
      this.isrRequest = false
    },
    // 获取父级分类列表
    getFatherCategory() {
      return ApiGategories().then(res => {
        this.fatherCategoryList = res.data
      })
    },
    // 获取时时汇率
    async getExchangeRates() {
      return exchangeRates().then(res => {
        this.realTimeRates = res.data.rates;
      })
    }
  }
})

export default useCartStore
